﻿namespace EyeOpen.Editing.Windows
{
	using System;
	using System.Linq.Expressions;
	using System.Reflection;

	public abstract class Validator<T> : 
		IValidator<T> where T : class
	{
		public abstract string Validate(T obj, string propertyName);

		public string Validate(object obj, string propertyName)
		{
			return Validate((T)obj, propertyName);
		}

		/// <exception cref="ArgumentException">The given expression is not a MemberExpression.</exception>
		protected string Property<TProperty>(Expression<Func<T, TProperty>> expression)
		{
			var memberExpression = expression.Body as MemberExpression;

			if (memberExpression == null)
			{
				throw new ArgumentException("The given expression is not a MemberExpression.", "expression");
			}

			var propertyInfo =
				memberExpression.Member as PropertyInfo;

			if (propertyInfo == null)
			{
				throw new ArgumentException("The given expression is not a property.", "expression");
			}

			return propertyInfo.Name;
		}
	}
}